package com.usaas.search.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usaas.common.core.domain.AjaxResult;
import com.usaas.search.dto.PlatCategoryDto;
import com.usaas.search.mapper.PlatCategoryMapper;
import com.usaas.search.pojo.PlatCategoryPojo;
import com.usaas.search.service.IPlatCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 平台分类Service业务层处理
 *
 * @author Helios
 * @date 2024-08-14
 */
@Slf4j
@Service
public class PlatCategoryServiceImpl extends ServiceImpl<PlatCategoryMapper, PlatCategoryPojo> implements IPlatCategoryService {

    @Resource
    private PlatCategoryMapper platCategoryMapper;

    /**
     * 查询平台分类
     *
     * @param id 平台分类主键
     * @return 平台分类
     */
    @Override
    public PlatCategoryDto queryById(Long id) {
        PlatCategoryPojo pojo = this.getById(id);
        if (pojo != null) {
            PlatCategoryDto dto = BeanUtil.copyProperties(pojo, PlatCategoryDto.class);
            List<PlatCategoryDto> dtos = ListUtil.of(dto);
            convertList(dtos);
            return dtos.get(0);
        }
        return null;
    }

    /**
     * 查询平台分类列表
     *
     * @param platCategoryDto 平台分类
     * @return 平台分类
     */
    @Override
    public List<PlatCategoryDto> queryList(PlatCategoryDto platCategoryDto) {
        LambdaQueryWrapper<PlatCategoryPojo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = this.buildWrapper(queryWrapper, platCategoryDto);
        if (queryWrapper == null) {
            return null;
        }
        List<PlatCategoryPojo> list = this.list(queryWrapper);
        if (CollectionUtil.isEmpty(list)) {
            return null;
        }

        List<PlatCategoryDto> dtoList = BeanUtil.copyToList(list, PlatCategoryDto.class);
        convertList(dtoList);
        return dtoList;
    }

    private void convertList(List<PlatCategoryDto> dtoList) {
        if (CollectionUtil.isEmpty(dtoList)) {
            return;
        }
    }

    /**
     * 新增平台分类
     *
     * @param platCategoryDto 平台分类
     * @return 结果
     */
    @Override
    @Transactional
    public boolean add(PlatCategoryDto platCategoryDto) {

        PlatCategoryPojo platCategoryPojo = BeanUtil.copyProperties(platCategoryDto, PlatCategoryPojo.class);
        return this.save(platCategoryPojo);
    }

    /**
     * 修改平台分类
     *
     * @param platCategoryDto 平台分类
     * @return 结果
     */
    @Override
    @Transactional
    public boolean update(PlatCategoryDto platCategoryDto) {
        PlatCategoryPojo platCategoryPojo = BeanUtil.copyProperties(platCategoryDto, PlatCategoryPojo.class);
        return this.updateById(platCategoryPojo);
    }

    /**
     * 批量删除平台分类
     *
     * @param ids 需要删除的平台分类主键
     * @return 结果
     */
    @Override
    @Transactional
    public boolean removeBatch(List<String> ids) {
        List<Long> idsLong = ids.stream().map(Long::parseLong).collect(Collectors.toList());
        return this.removeBatchByIds(idsLong);
    }


    /**
     * 分页查询${subTable.functionName}信息
     *
     * @param platCategoryDto
     * @return
     */
    @Override
    public AjaxResult queryPage(PlatCategoryDto platCategoryDto) {
        Page<PlatCategoryPojo> page = new Page<>(platCategoryDto.getPageNum(), platCategoryDto.getPageSize());
        LambdaQueryWrapper<PlatCategoryPojo> wrapper = new LambdaQueryWrapper<PlatCategoryPojo>();
        //此处可做前置判断
        if (wrapper == null) {
            return AjaxResult.success(page);
        }
        wrapper = buildWrapper(wrapper, platCategoryDto);
        if (wrapper == null) {
            return AjaxResult.success(page);
        }
        Page<PlatCategoryPojo> pageResult = this.page(page, wrapper);
        Page<PlatCategoryDto> finalResult = BeanUtil.copyProperties(pageResult, Page.class);
        List<PlatCategoryPojo> records = pageResult.getRecords();
        if (CollectionUtil.isNotEmpty(records)) {
            List<PlatCategoryDto> dtos = BeanUtil.copyToList(records, PlatCategoryDto.class);
            convertList(dtos);
            finalResult.setRecords(dtos);
        }
        return AjaxResult.success(finalResult);
    }
    @Override
    public Page<PlatCategoryDto> queryPage2(PlatCategoryDto platCategoryDto) {
        Page<PlatCategoryPojo> page = new Page<>(platCategoryDto.getPageNum(), platCategoryDto.getPageSize());
        LambdaQueryWrapper<PlatCategoryPojo> wrapper = new LambdaQueryWrapper<PlatCategoryPojo>();
        Page<PlatCategoryDto> finalResult = BeanUtil.copyProperties(page, Page.class);
        //此处可做前置判断
        if (wrapper == null) {
            return finalResult;
        }
        wrapper = buildWrapper(wrapper, platCategoryDto);
        if (wrapper == null) {
            return finalResult;
        }
        Page<PlatCategoryPojo> pageResult = this.page(page, wrapper);
         finalResult = BeanUtil.copyProperties(pageResult, Page.class);
        List<PlatCategoryPojo> records = pageResult.getRecords();
        if (CollectionUtil.isNotEmpty(records)) {
            List<PlatCategoryDto> dtos = BeanUtil.copyToList(records, PlatCategoryDto.class);
            convertList(dtos);
            finalResult.setRecords(dtos);
        }
        return finalResult;
    }

    private LambdaQueryWrapper<PlatCategoryPojo> buildWrapper(LambdaQueryWrapper<PlatCategoryPojo> wrapper, PlatCategoryDto param) {
        log.info("buildPageWrapper param :{}", param);
        if (wrapper == null) {
            return null;
        }
        Long id = param.getId();
        if (id != null) {
            wrapper.eq(PlatCategoryPojo::getId, id);
        }
        Long parentId = param.getParentId();
        if (parentId != null) {
            wrapper.eq(PlatCategoryPojo::getParentId, parentId);
        }
        String categoryName = param.getCategoryName();
        if (StrUtil.isNotBlank(categoryName)) {
            wrapper.like(PlatCategoryPojo::getCategoryName, categoryName);
        }
        String categoryIcon = param.getCategoryIcon();
        if (StrUtil.isNotBlank(categoryIcon)) {
            wrapper.eq(PlatCategoryPojo::getCategoryIcon, categoryIcon);
        }
        String skinInfo = param.getSkinInfo();
        if (StrUtil.isNotBlank(skinInfo)) {
            wrapper.eq(PlatCategoryPojo::getSkinInfo, skinInfo);
        }
        Long categorySort = param.getCategorySort();
        if (categorySort != null) {
            wrapper.eq(PlatCategoryPojo::getCategorySort, categorySort);
        }
        String createBy = param.getCreateBy();
        if (StrUtil.isNotBlank(createBy)) {
            wrapper.eq(PlatCategoryPojo::getCreateBy, createBy);
        }
        Date createTime = param.getCreateTime();
        if (createTime != null) {
            wrapper.eq(PlatCategoryPojo::getCreateTime, createTime);
        }
        String updateBy = param.getUpdateBy();
        if (StrUtil.isNotBlank(updateBy)) {
            wrapper.eq(PlatCategoryPojo::getUpdateBy, updateBy);
        }
        Date updateTime = param.getUpdateTime();
        if (updateTime != null) {
            wrapper.eq(PlatCategoryPojo::getUpdateTime, updateTime);
        }
        String remark = param.getRemark();
        if (StrUtil.isNotBlank(remark)) {
            wrapper.eq(PlatCategoryPojo::getRemark, remark);
        }
        Long delFlag = param.getDelFlag();
        if (delFlag != null) {
            wrapper.eq(PlatCategoryPojo::getDelFlag, delFlag);
        }
        log.info("buildPageWrapper result :{}", wrapper);
        return wrapper;
    }
}
